//
//  LiteVideoGalleryView.m
//  GolfFriend
//
//  Created by vescky on 13-12-14.
//  Copyright (c) 2013年 vescky.org. All rights reserved.
//

#import "LiteVideoGalleryView.h"
#import "UIImageView+MJWebCache.h"
#import <AVFoundation/AVFoundation.h>

@interface LiteVideoGalleryView ()

@end

@implementation LiteVideoGalleryView

@synthesize videoCollection,identifyString,bgColor,delegate,margin,viewWidth,colCount,onlyResponsToDelegateMenthod;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [self initData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initData {
    if (!videoCollection || [videoCollection count] < 1) {
        return;
    }
    if (!margin) {
        margin = 10;
    }
    if (!viewWidth) {
        viewWidth = 320.0;
    }
    if (!colCount) {
        colCount = 1;
    }
    
    if (!bgColor) {
        bgColor = [UIColor clearColor];
    }
}

- (void)initView {
    
    int photosCount = [videoCollection count];//图片的数量
    int rowCount = photosCount / colCount;//数量 / 列数 = 行数
    float imgUnitWith = (viewWidth - (colCount + 1) * margin) / colCount * 1.0; //(总宽度 - 所有列margin宽总和) / 列数 = 单个图片的宽
    float imgUnitHeight = imgUnitWith;//宽与高相等
    CGFloat startX = margin;//(self.view.frame.size.width - 3 * imgUnitWith - 2 * margin) * 0.5;
    CGFloat startY = margin;
    
    CGRect rect = self.view.frame;
    rect.size.width = viewWidth;
    rect.size.height = imgUnitHeight * colCount + (colCount + 1) * margin;
    [self.view setFrame:rect];
    [self.view setBackgroundColor:bgColor];
    
    UIImage *placeholder = Default_Image_Normal;
    for (int i = 0; i < photosCount; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        [self.view addSubview:imageView];
        
        // 计算位置
        int row = i/rowCount;
        int column = i%colCount;
        CGFloat x = startX + column * (imgUnitWith + margin);
        CGFloat y = startY + row * (imgUnitHeight + margin);
        imageView.frame = CGRectMake(x, y, imgUnitWith, imgUnitHeight);
        NSLog(@"%@",NSStringFromCGRect(imageView.frame));
        // 下载图片
        [imageView setImageURLStr:[videoCollection objectAtIndex:i] placeholder:placeholder];
        
        // 事件监听
        imageView.tag = i;
        imageView.userInteractionEnabled = YES;
        [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage:)]];
        
        // 内容模式
        imageView.clipsToBounds = YES;
        imageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    
}

-(void)tapImage:(UITapGestureRecognizer *)tap
{
    
}

- (float)getViewHeight {
    int pCount = [videoCollection count];
    int rowCount = pCount / colCount;
    float imgUnitHeight = (viewWidth - (colCount + 1) * margin) / colCount * 1.0;
    return rowCount * imgUnitHeight + (rowCount+1)*margin;
}

+(UIImage *)getImage:(NSString *)videoURL
{
    NSDictionary *opts = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
    
    
    NSURL *url = [[NSURL alloc] initFileURLWithPath:videoURL];
    
    AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:url options:opts];
    
    AVAssetImageGenerator *generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:urlAsset];
    
    generator.appliesPreferredTrackTransform = YES;
    generator.maximumSize = CGSizeMake(600, 450);
    
    NSError *error = nil;
    CGImageRef img = [generator copyCGImageAtTime:CMTimeMake(10, 10) actualTime:NULL error:&error];
    UIImage *image = [UIImage imageWithCGImage: img];
    
    return image;
}


@end
